**************************************************************************************************************
**	Replication do-file for "Governmment Policies and the Emergence of an Ethnic Dimension in Party Systems"
**	Author: Maayan Mor
**  Purpose: Produce "ZP elections 1867.dta." This file is used in Appendix C.
**************************************************************************************************************

clear all
set more off

/* SET DIRECTORY */

import excel "ZP elections.xlsx", sheet("sheet1") firstrow clear

rename cst_h drunno_haege
drop if drunno_haege==.

** code religion
gen r_2=1 if religion_feb=="k"
gen r_8=1 if religion_aug=="k"
recode r_2 r_8 (.=0)
label var r_2 "religion february"
label var r_8 "religion august"

** code parties
rename party_feb p_2
rename party_aug p_8

keep drunno_haege province_rb p_2 p_8 r_2 r_8
save "ZP elections.dta", replace

********************************************************
** 	Import data on the percentage of Catholics per CST
*******************************************************

**		Import the CST_ID-Kreis key
import excel "kuhne kreiskey1864.xls", sheet("kuhene kreiskey1864") firstrow clear 
drop if haege_id==.

merge 1:1 kreiskey1864 using  "ipehd_1864_population.dta"
drop if _merge==2 /* This is Sigmaringen */ 
drop _merge 

merge 1:1 kreiskey1864 using  "ipehd_1864_pop_demo"
drop if haege_id==. /* This is Sigmaringen */ 

collapse (sum) rel1864_cat pop1864_tot, by(haege_id)
gen cathp_64nat=(rel1864_cat/pop1864_tot)*100
label var cathp_64nat "% Catholics"
keep haege_id cathp_64nat

** 		Berlin is one census unit but six CSTs, so I am creating five artificial constituencies
expand 6 in 31 /*BERLIN CST==31*/

replace haege_id=32 in 186
replace haege_id=33 in 187
replace haege_id=34 in 188
replace haege_id=35 in 189
replace haege_id=36 in 190

/********************************************************************
**		CST 92 and 104 are also missing from the Kreiskey. 
**		92 is the Breslau and 104 is Beuthen. Each is one census unit but two CSTs. 
		The solution is to create these observations from duplicating 91 and 103 
*********************************************************************/		

expand 2 in 86 /*BRESLA==91*/
replace haege_id=92 in 191

expand 2 in 97 /*BEUTHEN==103*/
replace haege_id=104 in 192

rename haege_id drunno_haege 

merge m:m drunno_haege using "ZP elections.dta"

drop _merge

**		Code parties into families
foreach var of varlist p_2 p_8{
replace `var'="C" if `var'=="KP" | `var'=="FKV"
replace `var'="NL" if `var'=="NLP" 
replace `var'="L" if `var'=="DFP" | `var'=="FV" |  `var'=="Altliberal" | `var'=="BKV" 
replace `var'="I" if `var'=="fraktionslos" | `var'=="fraktionslos " | `var'=="ADAV"
replace `var'="P" if `var'=="Pole"
}

save "ZP elections 1867.dta", replace
